package com.yt.order.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.yt.order.models.service.IEmpAddressService;
import com.yt.order.models.bo.EmpAddressSaveBO;
import com.yt.order.models.bo.EmpAddressBO;
import com.yt.order.models.dto.EmpAddressDTO;

/**
 * 员工地址表控制器
 *
 * @author zhou
 * @Date 2025-09-14 12:06:09
 */

@RestController
@RequestMapping("/cms/empAddress")
@Slf4j
@Api(value = "EmpAddressController",tags = "员工地址表接口")
public class EmpAddressController {

    @DubboReference
    private IEmpAddressService empAddressService;

    @ApiOperation(value = "添加员工地址表",notes = "添加员工地址表")
    @PostMapping(value = "/insert")
    public Result insert(EmpAddressSaveBO saveBO) {
        boolean flag = empAddressService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }
    @PostMapping("/save")
    @ApiOperation(value = "保存员工地址表",notes = "保存员工地址表")
    public Result<Boolean> save(@RequestBody EmpAddressSaveBO saveBO){
        //查询原表是否有数据
        Wrapper<EmpAddressDTO>wrapper = new EntityWrapper<>();
        wrapper.eq("emp_id",saveBO.getEmpId());
        EmpAddressDTO empAddressDTO = empAddressService.selectOne(wrapper);
        if(empAddressDTO == null){
            //添加
            boolean flag=empAddressService.add(saveBO);
            return new Result<>(flag);
        } else {
            //更新
            BeanUtils.copyProperties(saveBO,empAddressDTO);

            boolean flag=empAddressService.update(empAddressDTO,wrapper);

            return new  Result<>(flag);
        }
    }
}
